package com.cx.common.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;

/**
 * @author cx
 * @description: 文件操作工具
 * @date 2021/6/15 11:21
 */
public class FileOperationUtil {


    /**
     * 删除文件
     *
     * @param fileName 文件名
     */
    public static void deleteWaveFile(String fileName) {
        File datFile = new File(fileName);
        if (datFile.isFile() && datFile.exists()) {
            datFile.delete();
        }
    }


    /**
     * 删除文件夹
     */
    public static boolean deleteDirectory(String directoryName) {
        File file = new File(directoryName);
        if (file.exists() && file.isDirectory()) {
            return deleteDir(file);
        }
        return false;
    }

    /**
     * 递归删除目录下的所有文件及子目录下所有文件
     *
     * @param dir 将要删除的文件目录
     * @return boolean
     */
    private static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            if (children == null) {
                return dir.delete();
            } else {
                for (String aChildren : children) {
                    boolean success = deleteDir(new File(dir, aChildren));
                    if (!success) {
                        return false;
                    }
                }
            }
        }
        return dir.delete();
    }

    /**
     * 文件移动至新文件夹
     */
    public static void copyToNewDirectory(String fileName, String oldFilePath, String newFilePath) throws IOException {
        File oldFile = new File(oldFilePath + "\\" + fileName);
        File newFile = new File(newFilePath + "\\" + fileName);
        if (newFile.exists()) {
            newFile.delete();
        }
        if (oldFile.exists()) {
            Files.copy(oldFile.toPath(), newFile.toPath());
        }
    }

    /**
     * 保存文件
     *
     * @param data     字节数组
     * @param fileName 文件名
     */
    public static void saveFile(byte[] data, String fileName) {
        if (data != null && data.length != 0) {
            FileOutputStream fos = null;
            BufferedOutputStream bos = null;
            try {
                File file = new File(fileName);
                fos = new FileOutputStream(file);
                bos = new BufferedOutputStream(fos);
                bos.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (fos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }

        }
    }

    /**
     * 创建目录
     *
     * @param path 目录路径
     */
    public static boolean createDirectory(String path) {
        File waveFile = new File(path);
        if (!(waveFile.exists() && waveFile.isDirectory())) {
            return waveFile.mkdir();
        }
        return false;
    }
}

